package com.econtact.web.user;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.RedirectView;

import com.econtact.business.entity.User;
import com.econtact.business.service.UserService;
import com.framework.common.Constant;
import com.framework.util.Tooles;

@SuppressWarnings("all")
@RequestMapping("/user")
@Scope(value="request")
@Controller
public class UserController 
{
	
	

	

	@Resource
	public UserService userService;

	
	
	@RequestMapping(value = "/test")
	public String  test(HttpServletRequest request,ModelMap modelMap)
	{
		userService.insertUser(null);
		return null;
	}
	

	
	/**
	 * 注册用户
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/registration",method=RequestMethod.POST)
	public String  registration(HttpServletRequest request,ModelMap modelMap)
	{
		String name=request.getParameter("name");

		User user=new User();
		user.setName(name);
		user.setRemark("");
		
		userService.insertUser(user);
		
		
	
		System.out.println("用户名："+request.getParameter("name"));
		System.out.println("验证码："+request.getSession().getAttribute(Constant.ValidateImage));
		
		
		return "index";
	}
	
//	
//	/**
//	 * 用户登录
//	 * @param request
//	 * @param modelMap
//	 * @return
//	 */
//	@RequestMapping(value="/login",method=RequestMethod.POST)
//	public String login(HttpServletRequest request,ModelMap modelMap)
//	{
//		System.out.println("login");
//
//	
//		
//		User user=new User();
//		user.setCompany("asdf");
//		modelMap.put("user", user);
//
//		
//		return "main/home";
//	}
	
//	
//	/**
//	 * 用户登录
//	 * @param request
//	 * @param modelMap
//	 * @return
//	 */
//	@RequestMapping(value="/login",method=RequestMethod.POST)
//	public String login(HttpServletRequest request,ModelMap modelMap)
//	{
//		System.out.println("login");
//		return "main/home";
//	}
	
	
	/**
	 * 登录ajax验证用户和密码
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/loginValidateNameAndPwdAjax",method=RequestMethod.POST)
	public boolean loginValidateNameAndPwd(HttpServletRequest request)
	{
		System.out.println("loginValidateNameAndPwd");
		String loginName=request.getParameter("loginName");
		String pwd=request.getParameter("pwd");
		if(Tooles.isNotNull(loginName)&&Tooles.isNotNull(pwd))
		{
			//如果用户密码是对的
			if(userService.validateUser(loginName, pwd))
			{
				return false;
			}
		}
		return true;
	}
	
	
	
	/**
	 * ajax验证注册验证码
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/registrationValidateImageAjax",method=RequestMethod.POST)
	public boolean registrationValidateImage(HttpServletRequest request,HttpServletResponse response)
	{
		boolean bo=false;
		Object temp=request.getSession().getAttribute(Constant.ValidateImage);
		if(temp!=null)
		{
			String sessionValidate=temp.toString().trim();
			String inputValidate=request.getParameter("validate").trim();
			if(sessionValidate.equals(inputValidate)==false)
			{			
				bo=true;
			}
		}
		return bo;
	}
	
	
	/**
	 * ajax验证用户名是否存在
	 * @param request
	 * @param response
	 * @return
	 */
	/*@ResponseBody的注解，Spring3.0 MVC @ResponseBody的作用是把返回值直接写到HTTP response body里*/
	@ResponseBody
	@RequestMapping(value = "/validateNameAjax",method=RequestMethod.POST)
	public boolean registrationValidateNameIsExist(HttpServletRequest request)
	{
		boolean bo=true;
		System.out.println("registrationValidateNameIsExist");
		return bo;
	}
	
	
	
	
}
